// pages/allNews/allNews.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    newsArticle: []
  },

  //全部资讯
  getAllNewsArticle: function () {
    var that = this
    const db = wx.cloud.database()
    db.collection('news_article').orderBy('_updateTime','desc').get().then(res => {
      console.log(res.data)
      that.setData({
        newsArticle: res.data
      })
      wx.hideLoading();
    })
  },

  //收藏的资讯
  getStarNewsArticle: function () {
    var that = this
    wx.cloud.callFunction({
      name: 'getMyStar'
    }).then(res => {
      console.log(res.result)
      that.setData({
        newsArticle: res.result
      })
      wx.hideLoading()
    })
  },

  //历史阅读
  getHistoryNewsArticle: function () {
    var that = this
    wx.cloud.callFunction({
      name: 'getMyHistory'
    }).then(res => {
      console.log(res.result)
      that.setData({
        newsArticle: res.result
      })
      wx.hideLoading()
    })
  },

  showDetail: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    wx.showLoading({
      title: '加载中',
    })
    console.log(e.id)
    this.setData({
      id: e.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    switch (this.data.id) {
      case "0":
        this.getAllNewsArticle();
        wx.setNavigationBarTitle({
          title: "全部资讯"
        });
        break;
      case "1":
        this.getStarNewsArticle();
        wx.setNavigationBarTitle({
          title: "我的收藏"
        });
        break;
      case "2":
        this.getHistoryNewsArticle();
        wx.setNavigationBarTitle({
          title: "历史阅读"
        });
        break;
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})